import { getCaptcha } from '@/api/login'
import uuid from 'uuid/v4'
import { ValidationProvider, ValidationObserver } from 'vee-validate'
import { mapMutations, mapGetters } from 'vuex'
export default {
  components: {
    ValidationProvider,
    ValidationObserver
  },
  data () {
    return {
      code: '',
      svg: ''
    }
  },
  mounted () {
    this._initSid()
    this._getCaptcha()
  },
  computed: {
    ...mapGetters(['sid'])
  },
  methods: {
    async _getCaptcha () {
      const res = await getCaptcha(this.sid)
      if (res.code === 200) {
        this.svg = res.data
      }
    },
    _initSid () {
      let sid = ''
      if (localStorage.getItem('sid')) {
        sid = localStorage.getItem('sid')
      } else {
        sid = uuid()
        localStorage.setItem('sid', sid)
      }
      this.setSid(sid)
    },
    ...mapMutations({
      setSid: 'SET_SID'
    })
  },
}